<!-- 字典项编辑弹窗 -->
<template>
  <ele-modal
    width="460px"
    :visible="visible"
    :close-on-click-modal="true"
    :title="isUpdate ? '修改字典' : '添加字典'"
    @update:visible="updateVisible"
  >
    <el-form :model="form" ref="form" :rules="rules" label-width="96px">
      <el-form-item label="上级节点:" >
        <el-input disabled :maxlength="20" v-model="form.parentName" placeholder="请输入上级节点" />
      </el-form-item>
      <el-form-item label="代码:" >
         <el-input disabled :maxlength="20" v-model="form.code" placeholder="请输入字典代码" />
      </el-form-item>
      <el-form-item label="名称:" prop="name">
        <el-input clearable :maxlength="20" v-model="form.name" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="值:" prop="value">
          <el-input clearable :maxlength="20" v-model="form.value" placeholder="请输入字典值" />
      </el-form-item>
       <el-form-item label="索引:" >
         <el-input-number style="width:324px" v-model="form.orderIndex" placeholder="请输入字典索引" />
      </el-form-item>
      <el-form-item label="备注:">
        <el-input :rows="4" type="textarea" :maxlength="200" v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">保存</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { isNumber } from 'ele-admin';
  import { AddDict,Update,QueryAllChildDictView } from '@/api/systemmanager/dictionarymanager';

  const DEFAULT_FORM = {
    id: '',
    code:'',
    name:'',
    value:'',
    isSys:'',
    remark: '',
    parentId:'',
    parentName:'',
    parentCode:'',
    orderIndex: '',
    createUser:'',
    createDate:'',
    dataSource:'',
    createUserName:'',
  };

  export default {
    name: 'DictDataEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      // 字典父id
      parentId: String,
      // 字典父节点名称
      parentName: String,
      // 字典父节点代码
      parentCode: String,
    },
    data() {
      return {
        // 表单数据
        form: { ...DEFAULT_FORM },
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入字典名称',
              trigger: 'blur'
            }
          ],
          value:[
            {
              trigger: 'blur',
              validator: (rule, value, callback) => {
                if (this.form.parentCode == this.Constant.J_ZK) {
                  if (!isNumber(value)) {
                    callback('请输入数字类型');
                  }
                  else{
                    callback();
                  }
                }else{
                  callback();
                }
              }
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs['form'].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const saveOrUpdate = this.isUpdate? Update: AddDict;
          saveOrUpdate({...this.form})
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            this.$util.assignObject(this.form, this.data);
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
            this.form.parentId = this.data.parentId;
            this.form.parentCode = this.data.parentCode;
            this.form.parentName = this.data.parentName;
          }
        } else {
          this.$refs['form'].clearValidate();
          this.form = { ...DEFAULT_FORM };
        }
      }
    }
  };
</script>
